<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="人脸ID" prop="faceId">
        <el-input
          v-model="queryParams.faceId"
          placeholder="请输入人脸ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户添加的额外字段" prop="externalFields">
        <el-input
          v-model="queryParams.externalFields"
          placeholder="请输入用户添加的额外字段"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人脸所在的外部图片ID" prop="externalImageId">
        <el-input
          v-model="queryParams.externalImageId"
          placeholder="请输入人脸所在的外部图片ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="矩形框宽度。" prop="width">
        <el-input
          v-model="queryParams.width"
          placeholder="请输入矩形框宽度。"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="矩形框左上角纵坐标。" prop="topLeftY">
        <el-input
          v-model="queryParams.topLeftY"
          placeholder="请输入矩形框左上角纵坐标。"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="矩形框左上角横坐标。" prop="topLeftX">
        <el-input
          v-model="queryParams.topLeftX"
          placeholder="请输入矩形框左上角横坐标。"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="矩形框高度。" prop="height">
        <el-input
          v-model="queryParams.height"
          placeholder="请输入矩形框高度。"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:FaceSetFace:add']"
        >新增</el-button>
      </el-col>
      
      <!-- <el-col :span="1.5">
     
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:FaceSetFace:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:FaceSetFace:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="FaceSetFaceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="人脸ID" align="center" prop="faceId" />
      <el-table-column label="用户添加的额外字段" align="center" prop="externalFields" />
      <el-table-column label="人脸所在的外部图片ID" align="center" prop="externalImageId" />
      <el-table-column label="矩形框宽度。" align="center" prop="width" />
      <el-table-column label="矩形框左上角纵坐标。" align="center" prop="topLeftY" />
      <el-table-column label="矩形框左上角横坐标。" align="center" prop="topLeftX" />
      <el-table-column label="矩形框高度。" align="center" prop="height" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:FaceSetFace:edit']"
          >修改</el-button> -->
           
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:FaceSetFace:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改系统人脸集对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
           <el-form-item label="人脸图片" prop="face">
                    <div class="upload-container">
                      <div>
                      </div>
                      <el-upload
                        class="face-upload"
                        list-type="picture-card"
                        action="#"
                        :auto-upload="false"
                        :show-file-list="false"
                        name="files"
                        :on-change="handleSearchFaceChange"
                       accept="image/*,.jpg,.jpeg,.png,.gif,.bmp,.webp"
                      >
                      <img v-if="form.face.url" class="img" :src="form.face.url" alt="" srcset="">
                         <i v-else  class="el-icon-plus img"></i>
                        <div slot="tip" class="el-upload__tip">上传人脸图片</div>
                      </el-upload>
                    </div>
                  </el-form-item>
        <el-form-item label="人脸库id" prop="externalImageId">
          <el-input v-model="form.faceSetId" placeholder="请输入人脸所在的外部图片ID" />
        </el-form-item>
      
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
   

  </div>
</template>

<script>
import { listFaceSetFace, getFaceSetFace, delFaceSetFace, addFaceSetFace, updateFaceSetFace } from "@/api/system/FaceSetFace";

export default {
  name: "FaceSetFace",
  data() {
    return {
      options: [{
        value: '选项1',
        label: '前置机'
      }, {
        value: '选项2',
        label: '后置机'
      }, {
        value: '选项3',
        label: '人脸识别'
      }, {
        value: '选项4',
        label: '挂号机'
      }],
        value: '',
        xiafa: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 系统人脸集表格数据
      FaceSetFaceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        faceId: null,
        externalFields: null,
        externalImageId: null,
        width: null,
        topLeftY: null,
        topLeftX: null,
        height: null
      },
      // 表单参数
      form: {
        face:{},
        faceSetId: null,
      },
      test:[],
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
     handleXiaFa(row) {
      this.xiafa = true;
      this.title = "下发";
    },
     xiafaSubmit() {
      this.$modal.msgSuccess("下发成功");
      // this.reset();
      this.xiafa = false;
      this.value = ''
    },
    /** 查询系统人脸集列表 */
    getList() {
      this.loading = true;
      listFaceSetFace(this.queryParams).then(response => {
        this.FaceSetFaceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
         this.xiafa = false;
         this.value = ''
      this.reset();
    },
    // 表单重置
    reset() {
      // this.form = {
      //   face:{},
      //   externalFields: null,
       
      // };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加系统人脸集";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFaceSetFace(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改系统人脸集";
      });
    },
      handleSearchFaceChange(file, fileList) {
        // 当文件选择发生变化时触发
    console.log('当前选择的文件:', file);
    // 将原始文件对象保存到form中，注意这里要用file.raw获取原始文件
       this.form.face = file;
      // this.test =JSON.parse(JSON.stringify(file.raw))
      // this.form.face = file;
      this.test = file.raw
      // this.test.push(file.raw);
    },
    /** 提交按钮 */
    async submitForm() {
          // 检查是否已选择文件
    if (!this.form.face) {
        this.$message.error('请先选择要上传的人脸图片');
        return;
    }
    try {
        // 调用API上传
        const res = await  addFaceSetFace({file:this.test,faceSetId:this.form.faceSetId});
        console.log('上传成功:', res);
        this.$message.success('人脸添加成功');
         this.open = false;
              this.getList();
        // 可以在这里添加成功后的处理逻辑
    } catch (error) {
        console.error('上传失败:', error);
        this.$message.error('人脸添加失败');
    }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除系统人脸集编号为"' + ids + '"的数据项？').then(function() {
        return delFaceSetFace(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/FaceSetFace/export', {
        ...this.queryParams
      }, `FaceSetFace_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.img{
  width: 150px;
  height: 150px;
}
</style>
<style scoped>
.custom-dialog {
  border-radius: 8px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
}

.custom-dialog /deep/ .el-dialog__header {
  border-bottom: 1px solid #f0f0f0;
  padding: 15px 20px;
}

.custom-dialog /deep/ .el-dialog__title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.dialog-content {
  padding: 20px;
}

.full-width-select {
  width: 100%;
}

.dialog-tips {
  margin-top: 10px;
  color: #909399;
  font-size: 14px;
}

.dialog-tips i {
  margin-right: 5px;
}

.dialog-footer {
  text-align: center;
  padding: 15px 20px;
  border-top: 1px solid #f0f0f0;
}

.cancel-btn {
  width: 100px;
}

.confirm-btn {
  width: 100px;
  background-color: #409EFF;
  border-color: #409EFF;
}

.confirm-btn:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

.confirm-btn:disabled {
  background-color: #a0cfff;
  border-color: #a0cfff;
  cursor: not-allowed;
}

</style>